#!/bin/bash
#
# Copyright 2010-2011 Cedric Priscal
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

usage() {
	echo "Usage: $0 <device> <port> <command> [arg]..."
}

if [ -z "$2" ]; then
	usage
	exit 1
fi

# Parse arguments
DEVICE=$1
PORT=$2
COMMAND=$3
ARGS=$4

if [ ! -e $PORT ]; then
	echo Port $PORT not found !
	usage
	exit 1
fi

# Parse arguments
case $DEVICE in

lg)
	echo "kl 0 0" > $PORT
	case $COMMAND in
	power)
		case $ARGS in
		on)
			echo "ka 0 1" >$PORT
			;;
		off)
			echo "ka 0 0" >$PORT
			;;
		*)
			echo "Usage: power <on|off>"
			;;
		esac
		;;
	volume)
		case $ARGS in
		mute)
			echo "ke 0 0" >$PORT
			;;
		unmute)
			echo "ke 0 1" >$PORT
			;;
		up)
			echo "mc 0 2" >$PORT
			;;
		down)
			echo "mc 0 3" >$PORT
			;;
		*)
			echo "Usage: volume <mute|unmute|up|down>"
			;;
		esac
		;;
	backlight)
		case $ARGS in
		full)
			echo "jq 0 0" >$PORT	# energy saving off
			echo "kd 0 0" >$PORT	# screen mute off
			;;
		low)
			echo "jq 0 3" >$PORT	# energy saving maximum
			echo "kd 0 0" >$PORT	# screen mute off
			;;
		off)
			echo "kd 0 1" >$PORT	# screen mute on
			;;
		*)
			echo "Usage: backlight <full|low|off>"
			;;
		esac
		;;
	input)
		case $ARGS in
		dtv)
			echo "xb 0 00" >$PORT
			;;
		av1)
			echo "xb 0 20" >$PORT
			;;
		av2)
			echo "xb 0 21" >$PORT
			;;
		av3)
			echo "xb 0 22" >$PORT
			;;
		hdmi1)
			echo "xb 0 90" >$PORT
			;;
		hdmi2)
			echo "xb 0 91" >$PORT
			;;
		hdmi3)
			echo "xb 0 92" >$PORT
			;;
		*)
			echo "Usage: input <dtv|av1|av2|av3|hdmi1|hdmi2|hdmi3>"
			;;
		esac
		;;
	*)
		echo "Usage: <power|volume|backlight|input>"
		;;
	esac # case $COMMAND
	echo "kl 0 1" > $PORT
	;;

marantz)
	# Set port at 9600,N,8,1
	#stty -F $PORT speed 9600 -parenb cs8 -cstopb >/dev/null
	case $COMMAND in
	power)
		case $ARGS in
		on)
			echo -e "@PWR:2\r" >$PORT
			;;
		off)
			echo -e "@PWR:1\r" >$PORT
			;;
		*)
			echo -e "Usage: power <on|off>"
			;;
		esac
		;;
	volume)
		case $ARGS in
		mute)
			echo -e "@AMT:2\r" >$PORT
			;;
		unmute)
			echo -e "@AMT:1\r" >$PORT
			;;
		up)
			echo -e "@VOL:1\r" >$PORT
			;;
		down)
			echo -e "@VOL:2\r" >$PORT
			;;
		*)
			echo "Usage: volume <mute|unmute|up|down>"
			;;
		esac
		;;
	input)
		case $ARGS in
		tv)
			echo -e "@SRC:1\r" >$PORT
			;;
		dvd)
			echo -e "@SRC:2\r" >$PORT
			;;
		vcr1)
			echo -e "@SRC:3\r" >$PORT
			;;
		vcr2)
			echo -e "@SRC:4\r" >$PORT
			;;
		aux1)
			echo -e "@SRC:9\r" >$PORT
			;;
		aux2)
			echo -e "@SRC:A\r" >$PORT
			;;
		cd)
			echo -e "@SRC:C\r" >$PORT
			;;
		cdr)
			echo -e "@SRC:D\r" >$PORT
			;;
		tape)
			echo -e "@SRC:E\r" >$PORT
			;;
		tuner)
			echo -e "@SRC:F\r" >$PORT
			;;
		fm)
			echo -e "@SRC:G\r" >$PORT
			;;
		am)
			echo -e "@SRC:H\r" >$PORT
			;;
		*)
			echo "Usage: input <tv|dvd|vcr1|vcr2|aux1|aux2|cd|cdr|tape|tuner|fm|am>"
			;;
		esac
		;;
	*)
		echo "Usage: <power|volume|input>"
		;;
	esac # case $COMMAND
	;;

*)
	echo "Valid devices: <lg|marantz>"
	;;

esac # case $DEVICE

